package com.hjm.app.fragment.store;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.ConsoleMessage;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.hjm.app.R;
import com.hjm.java.service.BaseService;

public class FragmentStoreHome extends Fragment {
    private static final String TAG = "FragmentStoreHome";
    private WebView webViewStoreHome;
    private int[] imageIds;
    private CallBack callBack;

    public FragmentStoreHome(CallBack callBack) {
        // Required empty public constructor
        this.callBack = callBack;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_store_home, container, false);
        webViewStoreHome = view.findViewById(R.id.web_view_store_home);

        WebSettings settings = webViewStoreHome.getSettings();
        settings.setBuiltInZoomControls(false);//设定是否显示缩放按钮
        settings.setSupportZoom(false);//设定是否支持缩放
        settings.setJavaScriptEnabled(true);// 启用JavaScript支持
        settings.setDomStorageEnabled(true);

        webViewStoreHome.loadUrl("file:///android_asset/store_home.html");

        /**
         * WebChromeClient类用于辅助WebView处理Javascript的对话框、网站图标、网站title、加载进度等。
         * 打印webview日志
         */
        webViewStoreHome.setWebChromeClient(new WebChromeClient() {
            @Override
            public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
                Log.d("WebView", "控制台信息: " + consoleMessage.message() + " -- From line "+ consoleMessage.lineNumber());
                return true; // 表示您已经处理了这条消息
            }
        });

        /**
         * WebViewClient类用于辅助WebView处理各种通知、请求事件。
         * 页面渲染完成时执行
         */
        webViewStoreHome.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
//                String javascriptCode = "loadImage('http://192.168.8.96:8080/api/image/13');";
//                webViewStoreHome.evaluateJavascript(javascriptCode, null);
                if (callBack!=null)
                    callBack.onPageFinished();
            }
        });

        Log.d(TAG, "onCreateView: store_home.html");
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    public void setImageIds(int[] imageIds){
        this.imageIds = imageIds;
        if (webViewStoreHome!=null){
            setHomeContent(imageIds);
        }
    }
    public void setHomeContent(int[] imageIds){
        Log.d(TAG, "setHomeContent: 设置店铺首页webView数据 "+imageIds);
        for (int i = 0; i < imageIds.length; i++){
            String javascriptCode = "loadImage('"+ BaseService.baseImgUrl+imageIds[i]+"');";
            webViewStoreHome.evaluateJavascript(javascriptCode, null);
        }
    }

    public void reloadWebViewStoreHome(){
        webViewStoreHome.reload();
    }

    public interface CallBack{
        void onPageFinished();
    }
}